package kz.pdt12.hw.tests;

import kz.pdt12.hw.data.ContactData;
import kz.pdt12.hw.utils.SortedListOf;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import static kz.pdt12.hw.tests.DataGenerator.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 07.03.13
 * Time: 1:31
 */
public class CreateContactTests extends TestBase {

    @DataProvider
    public Iterator<Object[]> contactsFromCsvFile() throws IOException, InstantiationException, IllegalAccessException {
        return wrapObjectsForDataProvider(loadObjectsFromCsvFile(new File("contacts.txt"), ContactData.class)).iterator();
    }

    @DataProvider
    public Iterator<Object[]> contactsFromXmlFile() throws IOException, InstantiationException, IllegalAccessException {
        return wrapObjectsForDataProvider(loadObjectsFromXmlFile(new File("contacts.xml"))).iterator();
    }

    @Test(dataProvider = "contactsFromXmlFile")
    public void testCreateContact(ContactData contact) {

        SortedListOf<ContactData> oldList = app.getModel().getContacts();

        app.getContactHelper().createContact(contact);

        SortedListOf<ContactData> newList = app.getModel().getContacts();

        assertThat(newList, equalTo(oldList));

        if (wantToCheck()) {
            if (app.getPropertyKey("check.db").equals("yes")) {
                assertThat(app.getModel().getContacts(), equalTo(app.getHibernateHelper().listContacts()));
            }
            if (app.getPropertyKey("check.ui").equals("yes")) {
                assertThat(app.getModel().getContacts(), equalTo(app.getContactHelper().getUiContacts()));
            }
        }
    }
}
